using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace TaskLogger
{
    public partial class ReportingForm : Form
    {
        public ReportingForm()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
        }

        private void buttonExport_Click(object sender, EventArgs e)
        {
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                StringBuilder report = GenerateReport(monthCalendar.SelectionStart, monthCalendar.SelectionEnd);
                File.WriteAllText(saveFileDialog.FileName, report.ToString(), Encoding.ASCII);
                Process.Start(saveFileDialog.FileName);
            }
        }

        public static StringBuilder GenerateReport(DateTime fromDate, DateTime toDate)
        {
            StringBuilder report = new StringBuilder();
            report.AppendLine("TaskLogger Report");
            var timeLogged = DataManager.TimeTrackerData.Time.Where(tr => tr.Start >= fromDate && tr.End <= toDate); 
            foreach (var timeLog in timeLogged)
            {
                var line = timeLog.TaskRow.ProjectRow.Name + "," + timeLog.TaskRow.Name + "," + timeLog.TaskRow.CategoryRow.Name + "," + timeLog.TaskRow.AssignerRow.Name + "," + timeLog.Start.ToString() + "," + timeLog.End.ToString() + "," + timeLog.Seconds.ToString();
                report.AppendLine(line);
            }
            report.AppendLine();
            return report;
        }

        private void textBox_Validated(object sender, EventArgs e)
        {
            Properties.Settings.Default.Save();
        }

    }
}